////////////////////////////////////////////////////////////////////////////////
//
//	This file is part of Woozox, see <http://code.google.com/p/woozox/>.
//
//	Copyright 2008-2009 Florian David
//
//	Woozoo is free software: you can redistribute it and/or modify
//	it under the terms of the GNU Lesser General Public License as 
//	published by the Free Software Foundation, either version 3 of 
//	the License, or (at your option) any later version.
//
//	Woozoo is distributed in the hope that it will be useful,
//	but WITHOUT ANY WARRANTY; without even the implied warranty of
//	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
//	GNU General Public License for more details.
//
//	You should have received a copy of the GNU Lesser General Public 
//	License along with Woozoo. If not, see <http://www.gnu.org/licenses/>.
//
////////////////////////////////////////////////////////////////////////////////
package org.woozoo.xhtml.core.base.selector
{
	/**
	 * @private
	 * */
	internal final class SelectorParser
	{
		private var __selector : String;
		private var __selectorLen : int;
		
		private var __selectorUnit : Array;
		private var __selectorUnitLen : int;
		
		private var __analyse : String = "";
		private var __index : int = 0;
		private var __matchIndex : int = -1;
		private var __selectorFoundNum : int = 0;
		
		public function init(selector : String) : void {
			__selector = selector;
			__selectorLen = __selector.length;
			
			__selectorUnit = SelectorOperatorFactory.SELECTOR_UNIT;
			__selectorUnitLen = __selectorUnit.length;
		}
		
		public function get selectorFoundNum() : int {
			return __selectorFoundNum;
		}
		
		public function get index() : int {
			return __index;
		}

		public function get matchIndex() : int {
			return __matchIndex;
		}
		
		public function get analyse() : String {
			return __analyse;
		}
		
		public function next() : Boolean {
			__matchIndex = -1;
			__analyse = "";
			while (__index < __selectorLen) {
				var found : Boolean = false;
				var char : String = __selector.charAt(__index);
				__analyse += char;
				
				for (var i : int = 0; i < __selectorUnitLen; i++) {
					if (__analyse.match(__selectorUnit[i])) {
						__matchIndex = i;
						found = true;
						break;
					}
				}
				if (!found && matchIndex >= 0) {
					__analyse = __analyse.substring(0, __analyse.length - 1);
					break;
				}
				__index++;
			}
			__selectorFoundNum++;
			return __analyse ? !found : false;
		}
	}
}